package ALT.facade;

import ALT.create.SamCommCreator;
import ALT.entities.Entry;
import ALT.entities.Forum;
import ALT.entities.Member;
import java.util.ArrayList;
import java.util.List;

public class Community {

    private List<Member> communityMembers;
    private List<Forum> communityForen;

    public Community() {
        this.communityForen = new ArrayList<Forum>();
        this.communityMembers = new ArrayList<Member>();
    }

    public boolean login(String nickname, String password) {
        for (Member m : communityMembers) {
            if (m.getmNickname().equals(nickname)) {
                if (m.getmPassword().equals(password)) {
                    return true;
                }
            }
        }

        return false;
    }

    public Forum registerMemberInForum(Member member, Forum forum) {
        Forum f = findForum(forum.getmOID());
        Member m = findMember(member.getmNickname());

        if (f == null || m == null) {
            return null;
        }

        m.addSubscription(f);
        f.addMember(m);
        return f;
    }

    public Member findMember(String nickname) {
        for (Member m : communityMembers) {
            if (m.getmNickname().equals(nickname)) {
                return m;
            }
        }

        return null;
    }

    public Forum findForum(String id) {
        for (Forum f : communityForen) {
            if (f.getmOID().equals(id)) {
                return f;
            }
        }

        return null;
    }

    public List<Entry> findEntriesOfAuthor(String author) {
        List<Entry> result = new ArrayList<Entry>();
        for (Forum f : communityForen) {
            List<Entry> entries = f.getEntries();
            for (Entry e : entries) {
                if ((e.getWrittenBy().getmNickname()).equals(author)) {
                    result.add(e);
                }
            }
        }
        return result;
    }

    public List<Entry> findEntriesOfTitle(String title) {
        List<Entry> result = new ArrayList<Entry>();
        for (Forum f : communityForen) {
            List<Entry> entries = f.getEntries();
            for (Entry e : entries) {
                if ((e.getTitle()).equals(title)) {
                    result.add(e);
                }
            }
        }
        return result;
    }

    public Entry addEntry(String title, String text, Member author, Forum forum) throws Exception {
        Entry newEntry = new Entry(author, forum, title, text);
        forum.addEntry(newEntry);
        return newEntry;
    }

    public void addMember(Member m) throws Exception {
        if (findMember(m.getmNickname()) != null) {
            throw new Exception();
        }

        communityMembers.add(m);
    }

    public void removeMember(Member m) {
        communityMembers.remove(m);
    }

    public List<Member> getCommunityMembers() {
        return communityMembers;
    }

    public void addForum(Forum f) {
        communityForen.add(f);
    }

    public void removeForum(Forum f) {
        communityForen.remove(f);
    }

    public List<Forum> getCommunityForen() {
        return communityForen;
    }

    public SamCommCreator getCreator() {
        return SamCommCreator.getInstance();
    }
}
